
$('#selectList').on('change',(e) => {
    $('.selectItems').hide();
    $($('.selectItems')[e.target.value]).show()
})

$('#swap').off('click').on('click', () => {
    let from = $('input[name="from"]');
    let to = $('input[name="to"]');
    let temp = from.val();
    from.val(to.val());
    to.val(temp);
})

let showTicket = (response) => {
    let ticketDataDiv = $('#queryResult');
    ticketDataDiv.empty();
    let res = JSON.parse(response);
    if(res == null){
        alert("没有数据")
        return;
    }
    res.forEach(t => {
        let dom =
        `<div class="ticket">
            <div class="ticketInfo">
                <h3>${t.lineName}</h3>
                <div>
                    <span>出发地：${t.passby.split('-')[0]}</span>
                    <span>目的地：${t.passby.split('-')[t.siteNum-1]}</span>
                    <span>站点数：${t.siteNum}</span>
                    <span>剩余票数：${t.restNumber}</span>
                    <span>时间：暂无</span>
                </div>
                <div>途径站点：${t.passby}</div>
            </div>
            <div class="seatInfo">${
                t.level_price.split(',').map((e, i, arr) => {
                    let lvInfo = e.split('-');
                    return `<div class="seatLevel">
                        <div>${lvInfo[0]} : ${lvInfo[1]}元</div>
                        <div><button class="buyTicket button" onclick="purchaseTicket('${t.lineName} ${lvInfo[0]} : ${lvInfo[1]}','${lvInfo[2]}')">购买</button></div>
                        ${i < arr.length - 1 ? '<hr style="grid-column:1/3">' : ''}
                    </div>`;
                }).join('')
            }
            </div>
        </div>`;
        ticketDataDiv.append(dom);
        $('.ticket').off('click').on('click', function(e) {
            $(e.target).find('.seatInfo').slideToggle();
        });
    })
}

$("#line").click(() => {
    $.ajax({
        url: '/queryTicket',
        type: 'POST',
        data: {
            type: $('#selectList')[0].value,
            line: $("input[name='line']")[0].value
        },
        success: function(res) {
            showTicket(res);
        },
        error: function(xhr, status, error) {
            $('#queryResult').html('<p>错误</p>');
        }
    });
});

$("#fromto").click(() => {
    $.ajax({
        url: '/queryTicket',
        type: 'POST',
        data: {
            type: $('#selectList')[0].value,
            from: $("input[name='from']")[0].value,
            to: $("input[name='to']")[0].value
        },
        success: function(res) {
            console.log(res)
            showTicket(res);
        },
        error: function(xhr, status, error) {
            $('#queryResult').html('<p>错误</p>');
        }
    });
});

$("#start").click(() => {
    $.ajax({
        url: '/queryTicket',
        type: 'POST',
        data: {
            type: $('#selectList')[0].value,
            from: $("input[name='from']")[0].value
        },
        success: function(res) {
            console.log(res)
            showTicket(res);
        },
        error: function(xhr, status, error) {
            $('#queryResult').html('<p>错误</p>');
        }
    });
});

let transferTo = (type) => {
    $.ajax({
        url: '/queryTicket',
        type: 'POST',
        data: {
            transType: type,
        },
        success: function(res) {
            showTicket(res);
        },
        error: function(xhr, status, error) {
            $('#queryResult').html('<p>错误</p>');
        }
    });
    setCurPage();
}

let Curpage = 1;
let _setCurPage = (n) => {
    Curpage = n;
    $("#curpage").attr("placeholder",Curpage);
}

let setCurPage = () =>{
    $.ajax({
        url: '/queryTicket',
        type: 'POST',
        data: {
            type: "getCurPage"
        },
        success: (res) => {
            console.log(res)
            _setCurPage(res);
        },
        error: (xhr, status, error) => {
            console.log('error')
        }
    });
};

$('#first').click(()=>{
    transferTo('first');
});
$('#pre').click(()=>{
    transferTo('pre');
});
$('#next').click(()=>{
    transferTo('next');
});
$('#last').click(()=>{
    transferTo('last');
});
$('#curpage').change(()=>{
    transferTo('curpage');
});

let purchaseTicket = (tktInfo,tktID)=>{
    if(!loginCheck()) return;
    if(!confirm(`您确定要购买 ${tktInfo} 吗？`)) return;

    $.ajax({
        url: '/purchaseTicket',
        type: 'POST',
        data: {
            userid, tktID
        },
        success: function(res) {
            if(res == "true"){
                alert("购票成功");
            } else {
                alert("购票失败");
            }
            console.log(res)
        },
        error: function(xhr, status, error) {
            alert("购票失败");
        }
    });
}

$().ready(function(){
    $('#line').click();
    setCurPage();
})

let loginCheck = () => {
    if(userid == null || userid == ''){
        alert("请先登录");
        window.location.href = "/login";
        return false;
    }
    return true;
}

